﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace SendMailServer.FileMatch
{
    internal class FileMatchManager
    {
        public static bool SendFileMatchResource(ResMsg t2, WSocketHandle parentHandler, string data)
        {
            if (t2.MsgCode != "CheckState")
                return false;

            int startFileCode = 100000;
            if (string.IsNullOrEmpty(t2.Content) == false)
            {
                int.TryParse(t2.Content.Split('-')[0], out startFileCode);
            }

            if (t2.IsOk || (startFileCode > 0 && startFileCode <= 999))
            {
                try
                {
                    var fileCode = FileCodeMatch.GetFileCode(t2.Content, t2.AppName);
                    var fileName = Path.Combine(WSocketHandle.GetContendDir(t2.AppName), fileCode + ".txt");
                    if (File.Exists(fileName))
                    {
                        var str = File.ReadAllText(fileName);
                        parentHandler.SendDataEvent(str);
                    }
                    else
                    {
                        parentHandler.SendDataEvent(t2.MsgCode + ":" + t2.Content);
                    }
                    return true;
                }
                catch (Exception err)
                {
                    parentHandler.SendDataEvent(data + " --> 读取文件出错" + err.Message);
                }
            }
            else
            {
                Logger.WriteLine("用户请求数据，没有下载中" + t2.Content);
            }
            parentHandler.SendDataEvent(data + " -->" + t2.ToString());
            return true;
        }
    }
}